package org.farec.tcc.spring.controller;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.farec.tcc.dao.ClienteDAO;
import org.farec.tcc.dao.FaqDAO;
import org.farec.tcc.dao.PermissaoDAO;
import org.farec.tcc.modelo.Faq;
import org.farec.tcc.modelo.Permissao;
import org.farec.tcc.modelo.Usuario;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FaqController {

	
	@RequestMapping("/formFaq")
	public String formFaq(Model model, HttpServletRequest request)
	{		
		Connection connection = (Connection) request.getAttribute("conexao");
		
		HttpSession session = request.getSession();
		Usuario usuario = (Usuario) session.getAttribute("usuarioLogado");
		PermissaoDAO daoPermissao = new PermissaoDAO(connection);
		List<Permissao> permissaoList = daoPermissao.verificaPermissao(usuario,"item");
		
		for(Permissao p : permissaoList)
		{
			if(p.getModulo().equals("Faq") && p.getItem().equals("adicionar"))
			{
				String[] prioridades = {"Alta","Normal","Baixa"};
				List<Faq> faqList = new ArrayList<>();
				
				for(String str : prioridades)
				{
					Faq faq = new Faq();
					faq.setPrioridade(str);
					faqList.add(faq);
				}
				
				model.addAttribute("faqs", faqList);
				
				return "cadFaq";
			}
		}
		
		return "index";
	}
	
	@RequestMapping("/adicionarFaq")
	public String adiciona(Faq faq, Model model, HttpServletRequest request) throws SQLException
	{
		Connection connection = (Connection) request.getAttribute("conexao");
		
		HttpSession session = request.getSession();
		Usuario usuario = (Usuario) session.getAttribute("usuarioLogado");
		PermissaoDAO daoPermissao = new PermissaoDAO(connection);
		List<Permissao> permissaoList = daoPermissao.verificaPermissao(usuario,"item");
		
		for(Permissao p : permissaoList)
		{
			if(p.getModulo().equals("Faq") && p.getItem().equals("adicionar"))
			{
				FaqDAO daoFaq = new FaqDAO(connection);	
				Map<String, Object> params = new LinkedHashMap<String, Object>(); 
				
				if(daoFaq.getFaq(faq).isAlerta() == true) //Se existir faq no banco
				{
					String[] prioridades = {"Alta","Normal","Baixa"};
					List<Faq> faqList = new ArrayList<>();
					
					for(String str : prioridades)
					{
						Faq faq2 = new Faq();
						faq2.setPrioridade(str);
						faqList.add(faq2);
					}
					
					model.addAttribute("faqs", faqList);
					
					params.put("faq", true);
					return "cadFaq";
				}
				
				faq.setUsuario(usuario.getUsuario());
				
				daoFaq.adiciona(faq);
				return "redirect:listaFaq";
			}
		}
		
		return "index";
	}
	
	@RequestMapping("/listaFaq")
	public String lista(Model model, HttpServletRequest request)
	{
		Connection connection = (Connection) request.getAttribute("conexao");

		HttpSession session = request.getSession();
		Usuario usuario = (Usuario) session.getAttribute("usuarioLogado");
		PermissaoDAO daoPermissao = new PermissaoDAO(connection);
		List<Permissao> permissaoList = daoPermissao.verificaPermissao(usuario,"item");
		List<Permissao> permissaoList2 = new ArrayList<>();
		
		for(Permissao p : permissaoList)
		{
			permissaoList2.add(daoPermissao.getPermissao("Faq", p));
			
		}
		
		if(permissaoList2.size() > 0 )
		{
			FaqDAO daoFaq = new FaqDAO(connection);
			
			model.addAttribute("permissoesList", permissaoList2);
			model.addAttribute("faqs", daoFaq.getLista());
			return "faq";
		}
		
		return "index";		
	}
	/*
	@RequestMapping("/exibeFaq")
	public String mostra(int id, Model model, HttpServletRequest request)
	{
		Connection connection = (Connection) request.getAttribute("conexao");
		
		HttpSession session = request.getSession();
		Usuario usuario = (Usuario) session.getAttribute("usuarioLogado");
		PermissaoDAO daoPermissao = new PermissaoDAO(connection);
		List<Permissao> permissaoList = daoPermissao.verificaPermissao(usuario);
		
		for(Permissao p : permissaoList)
		{
			if(p.getModulo().equals("Faq") && p.getItem().equals("alterar"))
			{
				FaqDAO daoFaq = new FaqDAO(connection);
				model.addAttribute("faqs", daoFaq.buscaPorCodigo(id));
				return "editfaq";
			}
		}
		
		return "index";
	}
	
	@RequestMapping("/alterarFaq")
	public String altera(Faq local, Model model, HttpServletRequest request) throws SQLException
	{
		Connection connection = (Connection) request.getAttribute("conexao");
		
		FaqDAO daoFaq = new FaqDAO(connection);
		
		daoFaq.altera(local);
		return "redirect:listaFaq";
		
	}
	*/
	
	@RequestMapping("/removeFaq")
	public String remove(int codigo, HttpServletRequest request)
	{
		Connection connection = (Connection) request.getAttribute("conexao");
		
		HttpSession session = request.getSession();
		Usuario usuario = (Usuario) session.getAttribute("usuarioLogado");
		PermissaoDAO daoPermissao = new PermissaoDAO(connection);
		List<Permissao> permissaoList = daoPermissao.verificaPermissao(usuario,"item");
		
		for(Permissao p : permissaoList)
		{
			if(p.getModulo().equals("Faq") && p.getItem().equals("alterar"))
			{
				FaqDAO daoFaq = new FaqDAO(connection);
				daoFaq.remove(codigo);
				return "redirect:listaFaq";
			}
		}
		
		return "index";
	}
	
}
